import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/admin',
      name: 'admin',
      redirect: '/homeManage',
      component: () => import('../layout/adminLayout.vue'),
      children: [
        {
          path: '/BgManage',
          name: 'BgManage',
          component: () => import('../views/adminView/BgManage.vue')
        },
        {
          path: '/bDManage',
          name: 'bDManage',
          component: () => import('../views/adminView/BDManage.vue')
        },
        {
          path: '/noticeManage',
          name: 'noticeManage',
          component: () => import('../views/adminView/NoticeManage.vue')
        },
        {
          path: '/userManage',
          name: 'userManage',
          component: () => import('../views/adminView/UserManage.vue')
        },
        {
          path: '/workerManage',
          name: 'workerManage',
          component: () => import('../views/adminView/WorkerManage.vue')
        },
        {
          path: '/adManage',
          name: 'adManage',
          component: () => import('../views/adminView/AdManage.vue')
        },
        {
          path: '/repairManage',
          name: 'repairManage',
          component: () => import('../views/adminView/RepairManage.vue')
        },
        {
          path: '/leaveRequestManage',
          name: 'leaveRequestManage',
          component: () => import('../views/adminView/LeaveRequestManage.vue')
        }
      ]
    },
    {
      path: '/user',
      name: 'user',
      redirect: '/myRepair',
      component: () => import('../layout/userLayout.vue'),
      children: [
        {
          path: '/userInfo',
          name: 'userInfo',
          component: () => import('../views/userView/UserInfo.vue')
        },
        {
          path: '/myRepair',
          name: 'myRepair',
          component: () => import('../views/userView/MyRepair.vue')
        }
      ]
    },
    {
      path: '/worker',
      name: 'worker',
      redirect: '/workerInfo',
      component: () => import('../layout/workerLayout.vue'),
      children: [
        {
          path: '/workerInfo',
          name: 'workerInfo',
          component: () => import('../views/workerView/WorkerInfo.vue')
        },
        {
          path: '/repair',
          name: 'repair',
          component: () => import('../views/workerView/Repair.vue')
        },
        {
          path: '/leaveRequest',
          name: 'leaveRequest',
          component: () => import('../views/workerView/LeaveRequest.vue')
        }
      ]
    },
    {
      path: '/',
      name: 'home',
      component: () => import('../views/Home.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/retrievePassword',
      name: 'retrievePassword',
      component: () => import('../views/RetrievePassword.vue')
    },
    {
      path: '/adRequest',
      name: 'adRequest',
      component: () => import('../views/adRequestView/adRequest.vue')
    },
    {
      path: '/notice',
      name: 'notice',
      component: () => import('../views/Notice.vue')
    },
    {
      path: '/moreNotice',
      name: 'moreNotice',
      component: () => import('../views/MoreNotice.vue')
    }
  ]
})


// 导航守卫
router.beforeEach((to, from, next) => {
  // let isLogin = false
  const token = localStorage.getItem('Authorization');
  if (token || to.path === '/' || to.path === '/retrievePassword' || to.path === '/adRequest' || to.path === '/login' || to.path === '/notice' || to.path === '/moreNotice') {
    next()
  } else {
    ElMessage.warning('您还未登录哦')
    router.push('/login')
  }
})

export default router
